# -*- coding:utf-8 _*-  
""" 
@author:yanming
@file: creat_data.py 
@version:
@time: 2020/08/09 
@email:476588294@qq.com
@function： 
"""
import random
from pandas import read_excel, ExcelWriter
import os


def rename(src, dst):
    if dst in os.listdir(path):
        return False
    else:
        os.rename(src, os.path.join(path, dst))
        return True


def get_files(path):
    dh = ['00-0001', '00-0002', '00-0003', '00-0004', '00-0005', '00-0006', '00-0007', '00-0008', '00-0009', '00-0010']
    rwsh = ['ZD9990', 'ZD9991', 'ZD9992', 'ZD9993', 'ZD9994', 'ZD9995', 'ZD9996', 'ZD9997', 'ZD9998', 'ZD9999',
            'ZD9980', 'ZD9981', 'ZD9982', ]

    for i in range(100):
        random.shuffle(dh)
        random.shuffle(rwsh)
        try:
            for file in os.listdir(path):
                new_name = '%sBOM-%s-ADTS203D-1-ZB-V4.00-第2版.200312.xlsx' % (dh[0], rwsh[0])
                result = rename(os.path.join(path, file), new_name)
                random.shuffle(dh)
                random.shuffle(rwsh)
                if not result:
                    random.shuffle(dh)
                    random.shuffle(rwsh)
        except:
            continue


def fill_data(path):
    for file in os.listdir(path):
        print(file)
        dh = file[0:7]

        data = read_excel(os.path.join(path, file), na_values='', header=1, keep_default_na=False)
        data.loc[:, '代号'] = dh
        writer = ExcelWriter(os.path.join(path, file))

        data.to_excel(writer, sheet_name='清单', startrow=1, index=False)
        writer.save()


if __name__ == "__main__":
    path = r"E:\workspace\PDMs\data"
    get_files(path)
    fill_data(path)
    print('finnished!')
